import java.util.LinkedList;


/**
 * A global feature is a city, road, farm, cloister, river, etc.
 * A global feature (except cloister) points to the tile features which make it up. 
 * A global feature can be completed, after which it is immutable/frozen.
 * A global feature can possibly be merged with another of the same type.
 * 
 * Global features can also cache certain things, like which players meeples are on it...
 * Some global features, like rivers, can contain no meeples.
 * 
 * @author ben
 */
public class GlobalFeature {
	public LinkedList<Tile.Feature> features;
	public boolean isComplete = false;
	public int type; // see Tile feature types
	public int points;
	
	public LinkedList<Meeple> meeples;  // list of occupying meeples, if any	
	
	public GlobalFeature(int type)
	{
		this.type = type;		
		features = new LinkedList<Tile.Feature>();
		meeples = new LinkedList<Meeple>();
		this.points = 0;
	}
	
	public void addFeature(Tile.Feature f)
	{		 
		features.add(f);
		f.global = this;
		this.points += 1;
	}
	
	public void addMeeple(Meeple m)
	{
		meeples.add(m);
	}
	
	public void mergeWith(GlobalFeature gf)
	{
		assert(gf.type == type);
		features.addAll(gf.features);
		for(Tile.Feature f:gf.features)
		{
			f.global = this; 
		}
		this.meeples.addAll(gf.meeples);
		
		this.points += gf.points;
	}
	
	public boolean isComplete()
	{
		// perform a completion check
		
		// for farm, road and castle
		if (type==Tile.CITY || type==Tile.ROAD || type==Tile.FARM || type==Tile.RIVER)
		{
			for(Tile.Feature f: features)
				if (!f.isClosed()) return false;
		}
		else if (type==Tile.CLOISTER)
		{
			// merely check if all surrounding tiles are placed
		}	
				
		return true;		
	}
	
	/**
	 * Determines if global feature is occupied, ie, atleast one meeple is already placed 
	 * @return bool  
	 */
	public boolean isOccupied()
	{
		return !meeples.isEmpty();
	}
}
